/**
 * Project Name:alqsoft-ms
 * File Name:RedisUtils.java
 * Package Name:com.alqsoft.redis
 * Date:2016年10月19日下午8:33:02
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.alqsoft.redis;

import com.alqsoft.init.InitParamUtils;
import com.alqsoft.init.InitParams;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * ClassName:RedisUtils <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2016年10月19日 下午8:33:02 <br/>
 * @author   张灿
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
public class RedisUtils {
	
	private static JedisPool jedisPool = null;
	
	static{
	       JedisPoolConfig config = new JedisPoolConfig();
	       config.setMaxIdle(Integer.parseInt(InitParamUtils.getString(InitParams.getREDISMAXIDLE())));
	       config.setTestOnBorrow(Boolean.getBoolean(InitParamUtils.getString(InitParams.getTESTONBORROW())));
	       jedisPool = new JedisPool(config, InitParamUtils.getString(InitParams.getREDISIP()),Integer.parseInt(InitParamUtils.getString(InitParams.getREDISPORT())));
	}
	
    public synchronized static Jedis getJedis() {
         try {
             if (jedisPool != null) {
                 Jedis resource = jedisPool.getResource();
                 return resource;
             } else {
            	 return null;
             }
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         }
     }
    
	public static void returnResource(final Jedis jedis){
		
		if(jedis !=null){
			jedisPool.returnResource(jedis);
		}
	}
}
